#pragma once

#include <pthread.h>

namespace zmh
{
    class Mutex
    {
    public:
        Mutex()
        {
            pthread_mutex_init(&_mutex, nullptr);
        }
        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
        }
        void lock()
        {
            pthread_mutex_lock(&_mutex);
        }
        void unlock()
        {
            pthread_mutex_unlock(&_mutex);
        }
        pthread_mutex_t *get_mutex()
        {
            return &_mutex;
        }

    private:
        pthread_mutex_t _mutex;
    };

    class auto_Mutex
    {
    public:
        auto_Mutex(Mutex &mutex)
            : _mutex(mutex.get_mutex())
        {
            pthread_mutex_lock(_mutex);
        }
        ~auto_Mutex()
        {
            pthread_mutex_unlock(_mutex);
        }

    private:
        pthread_mutex_t *_mutex;
    };
}